package com.dede.applib.extends

import android.app.Fragment
import android.content.Context
import android.os.Build
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.annotation.IntRange
import android.support.annotation.StringRes
import android.support.design.internal.SnackbarContentLayout
import android.support.design.widget.Snackbar
import android.support.v4.content.ContextCompat
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.dede.applib.R


/**
 * @author hsh
 * @time 2017/7/15 015 上午 10:08.
 * @doc Snackbar扩展函数
 */
fun Snackbar.addView(view: View, @IntRange(from = 0) index: Int): Snackbar {
    /**
     * Snackbar内部维护了一个SnackbarContentLayout（详见make方法）,make方法创建了SnackbarContentLayout对象（继承于线性布局），
     * 并调用了私有的构造方法，构造方法内直接调用了父类BaseTransientBottomBar的构造方法，
     * 父类BaseTransientBottomBar内部维护了一个继承于帧布局布局的SnackbarBaseLayout（详见BaseTransientBottomBar构造方法），
     * 并将Snackbar中make方法中创建的SnackbarContentLayout对象添加到SnackbarBaseLayout的第一个位置。
     *
     * Snackbar的getView方法调用的父类BaseTransientBottomBar的方法，在父类中直接返回了构造方法中创建的SnackbarBaseLayout对象
     */
    val snackBarLayout = ((this.view as ViewGroup).getChildAt(0)) as SnackbarContentLayout
    val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    layoutParams.gravity = Gravity.CENTER_VERTICAL
    snackBarLayout.addView(view, index, layoutParams)
    return this
}

fun Snackbar.addView(view: View): Snackbar {
    addView(view, 0)
    return this
}

fun Snackbar.addImage(@DrawableRes resId: Int): Snackbar {
    val imageView = ImageView(context)
    imageView.setImageResource(resId)
    addView(imageView)
    return this
}

fun Snackbar.showWarnImage(): Snackbar {
    addImage(R.drawable.ic_snackbar_warn)
    return this
}


fun Snackbar.color(@ColorRes colorRes: Int): Snackbar {
    this.view.setBackgroundColor(ContextCompat.getColor(this.view.context, colorRes))
    return this
}

fun Snackbar.colorShow(@ColorRes colorRes: Int) {
    this.view.setBackgroundColor(ContextCompat.getColor(this.view.context, colorRes))
    this.show()
}


fun Snackbar.textColor(@ColorRes colorRes: Int): Snackbar {
    (this.view.findViewById(R.id.snackbar_text) as TextView)
            .setTextColor(ContextCompat.getColor(view.context, colorRes))
    return this
}

fun Snackbar.actionTextColor(@ColorRes colorRes: Int): Snackbar
        = setActionTextColor(ContextCompat.getColor(view.context, colorRes))


fun Snackbar.setTextShow(message: CharSequence) {
    setText(message).show()
}

fun Snackbar.setTextShow(@StringRes resId: Int) {
    setText(resId).show()
}


fun Snackbar.warn(): Snackbar = this.color(R.color.colorWarn)

fun Snackbar.warnShow() = this.warn().show()


fun Snackbar.action(@StringRes resId: Int, listener: View.OnClickListener): Snackbar {
    return setAction(resId, listener)
}

fun Snackbar.action(@StringRes resId: Int, onClick: (() -> Unit?)? = null): Snackbar {
    return setAction(resId, { onClick?.invoke() })
}

fun Snackbar.actionShow(@StringRes resId: Int, listener: View.OnClickListener) {
    setAction(resId, listener).show()
}

fun Snackbar.actionShow(@StringRes resId: Int, onClick: (() -> Unit?)? = null) {
    setAction(resId, { onClick?.invoke() }).show()
}


fun Snackbar.action(text: CharSequence, listener: View.OnClickListener): Snackbar {
    return setAction(text, listener)
}

fun Snackbar.action(text: CharSequence, onClick: (() -> Unit?)? = null): Snackbar {
    return setAction(text, { onClick?.invoke() })
}

fun Snackbar.actionShow(text: CharSequence, listener: View.OnClickListener) {
    setAction(text, listener).show()
}

fun Snackbar.actionShow(text: CharSequence, onClick: (() -> Unit?)? = null) {
    setAction(text, { onClick?.invoke() }).show()
}


fun Fragment.makeSnackBar(view: View, text: CharSequence): Snackbar =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.makeSnackBar(view, text)
        } else {
            activity.makeSnackBar(view, text)
        }

fun Fragment.makeSnackBarL(view: View, text: CharSequence): Snackbar =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.makeSnackBarL(view, text)
        } else {
            activity.makeSnackBarL(view, text)
        }

fun Fragment.snackBar(view: View, text: CharSequence) =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.snackBar(view, text)
        } else {
            activity.snackBar(view, text)
        }

fun Fragment.snackBarL(view: View, text: CharSequence) =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.snackBarL(view, text)
        } else {
            activity.snackBarL(view, text)
        }


fun Context.snackBar(view: View, text: CharSequence)
        = Snackbar.make(view, text, Snackbar.LENGTH_SHORT).show()

fun Context.snackBarL(view: View, text: CharSequence)
        = Snackbar.make(view, text, Snackbar.LENGTH_LONG).show()

fun Context.makeSnackBar(view: View, text: CharSequence): Snackbar
        = Snackbar.make(view, text, Snackbar.LENGTH_SHORT)

fun Context.makeSnackBarL(view: View, text: CharSequence): Snackbar
        = Snackbar.make(view, text, Snackbar.LENGTH_LONG)

